package com.lagou.edu.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Properties;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@RestController
@RequestMapping("/email")
@RefreshScope
public class EmailController {

    @Value("${email.from}")
    private String from;   // 发送者邮箱
    @Value("${email.password}")
    private String password;  //授权码
    @Value("${email.host}")
    private String host;  //邮件服务器主机名
    @Value("${email.port}")
    private int port;  //邮件服务器端口
    @Value("${email.auth}")
    private String auth;  //指定客户端是否要向邮件服务器提交验证

    @GetMapping("/{email}/{code}")
    public boolean sendEmail(@PathVariable String email,@PathVariable String code){
        boolean send = sendMail(email, code);
        return send;
    }

    public boolean sendMail(String emailAddress, String emailMsg) {

        // 发给谁
        String to = emailAddress;

        // 定义properties对象，设置环境信息
        Properties properties = new Properties();

        /*
         * mail.smtp.host ：指定连接的邮件服务器的主机名。如：163邮箱就填写smtp.163.com
         * 若在本地测试的话，需要在本地安装smtp服务器
         */
        properties.setProperty("mail.smtp.host", host);

        // mail.smtp.auth：指定客户端是否要向邮件服务器提交验证
        properties.setProperty("mail.smtp.auth", auth);

        /*
         * mail.transport.protocol：指定邮件发送协议：smtp。smtp：发邮件；pop3：收邮件
         * mail.store.protocol:指定邮件接收协议
         */
        properties.setProperty("mail.transport.protocol", "smtp");

        // 获取session对象
        Session session = Session.getInstance(properties);

        // 当设置为true，JavaMail AP就会将其运行过程和邮件服务器的交互命令信息输出到console中，用于JavaMail的调试
        session.setDebug(true);
        try {

            // 创建邮件对象
            MimeMessage message = new MimeMessage(session);

            // 设置邮件发送方
            message.setFrom(new InternetAddress(from));

            // 设置邮件发送的主题<邮件标题>
            message.setSubject("邮件发送设置");

            // 设置邮件发送的内容
            String msg = "您好：您的注册验证码为：" + emailMsg + ", 该验证码10分钟内有效，请尽快注册！";
            message.setContent(msg, "text/html;charset=utf-8");
            Transport transport = session.getTransport();

            // 连接邮件服务器，“”中填写邮件服务器主机名
            transport.connect(host, port, from, password);
            transport.sendMessage(message, new Address[]{new InternetAddress(to)});
            transport.close();
            return true;
        } catch (MessagingException e) {
//            e.printStackTrace();
            return false;
        }
    }
}
